/** ***********************************************************************************
* @file         power_task.c
* @brief        系统电源管理任务
* @details      主要用来管理整个系统的工作状态
* @author       杨小珛
* @date         2021/01/09
* @copyright    北京高知科技有限公司    
* @version      V1.0
* @attention    无
**************************************************************************************/

#include "power_task.h"
#include "mon_task.h"
#include "log_task.h"
#include "com_lib.h"

#include "gsen_task.h"
#include "event_task.h"
#include "gnss_task.h"
#include "gaoz_api.h"

GaozMutexHandle_t   power_man_mutex;                //互斥操作
GaozMutexHandle_t   power_wakeup_mutex;             //互斥操作
GaozSemaphoreHandle_t   power_reveive_semaphore;    /*电源管理接受信号量*/

static uint8_t power_manage_list_count;
static power_system_man_s power_man_list[TASK_POWER_NUM]; 
static uint32_t power_man_sleep_time_tick; 

power_man_wakeup_source_list_t power_man_wakeup_source_list;
power_man_sleep_time_manage_t power_man_sleep_time_manage;

static void power_task_list_init(void);
static void power_task_manager( void * pvParameters);
static void power_man_semaphore_rev_create(void);
static uint8_t power_man_semaphore_get( uint32_t wait_time );
static uint8_t power_man_power_id_get( uint8_t *task_name );
static void power_man_sys_run_state_set( task_modules_e state , uint8_t *task_name );
static uint8_t power_man_sys_runing_state_check( task_modules_e state , uint8_t *task_name );

static uint8_t power_man_sys_acc_state(void);
static uint8_t power_man_sys_veh_state(void);

static uint8_t power_man_sys_rtc_state(void);
static uint8_t power_man_sys_ble_state(void);
static uint8_t power_man_sys_net_state(void);
static uint8_t power_man_sys_gsen_state(void);
static uint8_t power_man_sys_can_state(void);
static uint8_t power_man_sys_time_state(void);
static gsen_bmi160_body_state_e power_man_sys_body_state(void);

static uint32_t power_man_bat_sleep_mode( void );
static uint32_t power_man_net_run_mode( void );

static uint32_t power_man_can_sleep_mode( void );
static uint32_t power_man_can_run_mode( void );

static uint32_t power_man_gsen_run_mode( void );
static uint32_t power_man_ble_run_mode( void );
static uint32_t power_man_ble_sleep_mode( void );

static uint32_t power_man_sys_wakeup_source_init_state( void );
static void power_man_sys_wakeup_source_clear(void);

static void power_man_sleep_time_manage_init( void);
static uint32_t power_man_sys_wakeup_sleep_timeout(void);
static uint32_t power_man_sys_wakeup_run_timeout(void);

static uint32_t power_man_gps_run_mode( void );
static void power_man_run_time_manage_init( void);

static void power_man_sys_wakeup_source_sleeping( void );
/** ****************************************************************************
* @remarks       static void power_task_list_init(void)
* @brief         初始化系统任务电源管理列表参数
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void power_task_list_init(void)
{
    power_manage_list_count = 0;
    memset((uint8_t *)power_man_list , 0x00 , sizeof(power_system_man_s) * TASK_POWER_NUM );
    memset((uint8_t *)&power_man_wakeup_source_list , 0x00 , sizeof(power_man_wakeup_source_list_t));
    memset((uint8_t *)&power_man_sleep_time_manage , 0x00 , sizeof(power_man_sleep_time_manage_t));
}
    
/** ****************************************************************************
* @remarks       void power_man_create_task(unsigned portBASE_TYPE uxPriority)
* @brief         创建系统电源管理任务
* @param[in]     uxPriority 任务优先级
* @param[out]    无
* @return        无
* @attention     在系统启动后进行创建
*******************************************************************************/
void power_man_create_task(unsigned portBASE_TYPE uxPriority)
{   
    power_man_sleep_time_tick = 0;
    power_task_list_init();
    power_man_mutex = gaoz_mutex_create();/*创建电源管理互斥体*/
    if(power_man_mutex == 0)
    {
        while(1); /*创建失败*/
    }
    power_wakeup_mutex  = gaoz_mutex_create();/*创建电源管理互斥体*/
    if(power_wakeup_mutex == 0)
    {
        while(1); /*创建失败*/
    }
    
    power_man_semaphore_rev_create();
    gaoz_thread_create(power_task_manager, POW_MAN_NAME, 1024*2, NULL, uxPriority);
}

/** ****************************************************************************
* @remarks       static void power_man_semaphore_rev_create(void)
* @brief         系统电源管理信号量创建
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void power_man_semaphore_rev_create(void)
{
    power_reveive_semaphore = gaoz_sem_new(0);
    if( power_reveive_semaphore == 0 )
    {
        while(1);
    }
}

/** ****************************************************************************
* @remarks       static uint8_t power_man_semaphore_get( uint32_t wait_time )
* @brief         系统电源管理信号处理
* @param[in]     wait_time 等待信号的时间 单位S
* @param[out]    无
* @return        1 等待信号成功  ， 0 等待信号失败，超时等待
* @attention     无
*******************************************************************************/
static uint8_t power_man_semaphore_get( uint32_t wait_time )
{
    if(power_reveive_semaphore == 0)
    {
        power_man_semaphore_rev_create();
    }
    if( gaoz_sem_try_wait( power_reveive_semaphore, wait_time ) == true ) //等待回应信号量
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

/** ****************************************************************************
* @remarks       void power_man_semaphore_put(void)
* @brief         系统电源管理信号发送
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void power_man_semaphore_put(void)
{
    if(power_reveive_semaphore != 0)
    {
        gaoz_sem_signal( power_reveive_semaphore );
    }
}

/** ****************************************************************************
* @remarks       uint32_t power_man_task_reg( uint8_t *task_name, uint8_t * power_id )
* @brief         注册任务管理器
* @param[in]     *task_name 任务名称
* @param[out]    * power_id 电源管理ID地址
* @return        pdPASS 创建成功 ， pdFAIL 创建失败
* @attention     必须在任务中执行此函数
*******************************************************************************/
uint32_t power_man_task_reg( uint8_t *task_name, uint8_t * power_id )
{
    uint32_t i = 0;
    uint16_t res = pdFAIL;
    uint32_t task_id = 0;
    uint32_t osR = 0;
    
    if( power_manage_list_count >= TASK_POWER_NUM )
    {
        return(res);
    }
    if( power_id == NULL )
    {
        return(res);
    }
    if( power_man_mutex != 0)
    {
        gaoz_mutex_lock( power_man_mutex );
        osR = 1;
    }
    task_id = gaoz_get_thread_id();
    for(i = 0; i < TASK_POWER_NUM; i++) //先判断是否已经存在
    {
        if(power_man_list[i].task_id == task_id)
        {
            power_man_list[i].task_state = TASK_MODE_INIT;
            power_man_list[i].save_state = TASK_MODE_INIT;
            res = pdPASS;
        }
    }
    if( res != pdPASS )
    {
        for(i = 0; i < TASK_POWER_NUM; i++)//查找空间
        {
            if( power_man_list[i].task_id == 0 )
            {
                power_man_list[i].task_id = task_id;
                power_man_list[i].task_state = TASK_MODE_INIT;
                power_man_list[i].save_state = TASK_MODE_INIT;
                power_man_list[i].task_name = (uint8_t*)task_name;
                power_manage_list_count++;
                res = pdPASS;
                *power_id = i;
                break;
            }
        }
    }
    if( res == pdPASS )
    {
        log_debug_printf(INFOR_MODE,POW_MAN_NAME,"Power management task registered successfully,task_name,task_id,power_id:%s,%x,%d\r\n",task_name,task_id,*power_id);
    }
    if(osR != 0)
    {
        gaoz_mutex_unlock( power_man_mutex );
    }
    return(res);
}

/** ****************************************************************************
* @remarks       uint32_t power_man_remove_task(void)
* @brief         删除任务管理器
* @param[in]     无
* @param[out]    无
* @return        pdPASS 删除成功 ， pdFAIL 删除失败
* @attention     必须在任务中执行此函数
*******************************************************************************/
uint32_t power_man_remove_task(void)
{
    uint16_t  res = pdFAIL;
    uint32_t  i = 0;
    uint32_t  task_id = 0;
    uint32_t  osR = 0;

    if( power_manage_list_count >= TASK_POWER_NUM )
    {
        return(res);
    }
    if( power_man_mutex != 0)
    {
        gaoz_mutex_lock( power_man_mutex );
        osR = 1;
    }
    task_id = gaoz_get_thread_id();
    for(i = 0; i < TASK_POWER_NUM; i++)
    {
        if(power_man_list[i].task_id == task_id)
        {
            power_man_list[i].task_id = 0;
            power_man_list[i].task_state = TASK_MODE_INIT;
            power_man_list[i].save_state = TASK_MODE_INIT;
            power_man_list[i].task_name = NULL;
            power_manage_list_count--;
            res = pdPASS;
            break;
        }
    }
    
    if(osR != 0)
    {
        gaoz_mutex_unlock( power_man_mutex );
    }
    return(res);
}

/** ****************************************************************************
* @remarks       static uint8_t power_man_power_id_get( uint8_t *task_name )
* @brief         获取电源管理任务ID信息
* @param[in]     *task_name 任务名称
* @param[out]    无
* @return        电源管理的ID信息
* @attention     无
*******************************************************************************/
static uint8_t power_man_power_id_get( uint8_t *task_name )
{
    uint8_t  i = 0 , ret = POW_MAN_UNREG;
    for( i = 0; i < TASK_POWER_NUM; i++ )
    {
        if( power_man_list[i].task_id != 0 )
        {
            if( strstr(( const char* )power_man_list[i].task_name, ( char* )task_name ) != NULL )
            {
                ret = i;
                break;
            }
        }
    }
    return ret;
}

/** ****************************************************************************
* @remarks       task_modules_e power_man_task_state_get( uint8_t power_id )
* @brief         获取系统任务当前的状态
* @param[in]     power_id 电源管理ID信息
* @param[out]    无
* @return        任务的状态
* @attention     无
*******************************************************************************/
task_modules_e power_man_task_state_get( uint8_t power_id )
{
    task_modules_e  res = TASK_MODE_INIT;
    uint32_t osR = 0;
    if( power_man_mutex != 0)
    {
        gaoz_mutex_lock( power_man_mutex );
        osR = 1;
    }
    if( power_man_list[power_id].task_id != 0 )
    {
        res = power_man_list[power_id].task_state;
    }
    if(osR != 0)
    {
        gaoz_mutex_unlock( power_man_mutex );
    }
    return( res );
}

/** ****************************************************************************
* @remarks       void power_man_task_state_set(uint8_t power_id , task_modules_e state)
* @brief         设置当前任务保存的状态
* @param[in]     power_id :任务电源管理的ID 
* @param[in]     state :任务的状态
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void power_man_task_state_set(uint8_t power_id , task_modules_e state)
{
    uint32_t    osR = 0;
    if( power_man_mutex != 0)
    {
        gaoz_mutex_lock( power_man_mutex );
        osR = 1;
    }
    if( power_man_list[power_id].task_id != 0 )
    {
        power_man_list[power_id].task_state = state;
    }
    if(osR != 0)
    {
        gaoz_mutex_unlock( power_man_mutex );
    }
}

/** ****************************************************************************
* @remarks       task_modules_e power_man_save_state_get( uint8_t power_id )
* @brief         获取执行任务保存的状态
* @param[in]     power_id 电源管理ID信息
* @param[out]    无
* @return        当前任务的状态 task_modules_e
* @attention     无
*******************************************************************************/
task_modules_e power_man_save_state_get( uint8_t power_id )
{
    task_modules_e  res = TASK_MODE_INIT;
    uint32_t osR = 0;
    if( power_man_mutex != 0)
    {
        gaoz_mutex_lock( power_man_mutex );
        osR = 1;
    }
    if( power_man_list[power_id].task_id != 0 )
    {
        res = power_man_list[power_id].save_state;
    }
    if(osR != 0)
    {
        gaoz_mutex_unlock( power_man_mutex );
    }
    return( res );
}

/** ****************************************************************************
* @remarks       void power_man_save_state_set(uint8_t power_id , task_modules_e state)
* @brief         设置当前任务保存的状态
* @param[in]     power_id :任务电源管理的ID
* @param[in]     state :任务的状态
* @param[out]    无
* @return        当前任务的状态 task_modules_e
* @attention     无
*******************************************************************************/
void power_man_save_state_set(uint8_t power_id , task_modules_e state)
{
    uint32_t    osR = 0;
    if( power_man_mutex != 0)
    {
        gaoz_mutex_lock( power_man_mutex );
        osR = 1;
    }
    if( power_man_list[power_id].task_id != 0 )
    {
        power_man_list[power_id].save_state = state;
    }
    if(osR != 0)
    {
        gaoz_mutex_unlock( power_man_mutex );
    }
}

/** ****************************************************************************
* @remarks       uint8_t power_man_count_get(void)
* @brief         获取电源管理任务的数量
* @param[in]     无
* @param[out]    无
* @return        创建电源管理任务的数量
* @attention     无
*******************************************************************************/
uint8_t power_man_count_get(void)
{   
    return power_manage_list_count;
}
    
/** ****************************************************************************
* @remarks       static void power_man_sys_run_state_set( task_modules_e state , uint8_t *task_name )
* @brief         设置系统总体的运行状态
* @param[in]     state 设置任务运行的状态
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void power_man_sys_run_state_set( task_modules_e state , uint8_t *task_name )
{
    uint32_t osR = 0;
    uint8_t i = 0 , power_id = 0;
    if( power_man_mutex != 0)
    {
        gaoz_mutex_lock( power_man_mutex );
        osR = 1;
    }
    if( task_name != NULL )
    {

        if( strstr(( const char* )task_name, SYS_ALL_NAME ) != NULL )
        {
            for(i = 0; i < TASK_POWER_NUM; i++)
            {   
                if( power_man_list[i].task_id != 0 ) 
                {
                    if( power_man_list[i].task_state != state )
                    {
                        power_man_list[i].task_state = state;
                    }
                }
            }
        }
        else
        {
            power_id = power_man_power_id_get(   task_name );
            if( power_id < TASK_POWER_NUM )
            {
                if( power_man_list[power_id].task_id != 0 ) 
                {
                    if( power_man_list[power_id].task_state != state )
                    {
                        power_man_list[power_id].task_state = state;
                    }
                }
            }
        }
    }
    
    if(osR != 0)
    {
        gaoz_mutex_unlock( power_man_mutex );
    }
}

/** ****************************************************************************
* @remarks       static uint8_t  power_man_sys_runing_state_check( task_modules_e state , uint8_t *task_name )
* @brief         检测任务运行状态
* @param[in]     state 设置任务运行的状态
* @param[out]    无
* @return        1:检测成功，0:检测失败
* @attention     无
*******************************************************************************/
static uint8_t  power_man_sys_runing_state_check( task_modules_e state , uint8_t *task_name )
{
    uint32_t osR = 0;
    uint8_t i = 0, ret = 0, dete_num = 0, power_id = 0;
    if( power_man_mutex != 0)
    {
        gaoz_mutex_lock( power_man_mutex );
        osR = 1;
    }
    
    if( task_name != NULL )
    {
        if( strstr(( const char* )task_name, SYS_ALL_NAME ) != NULL )
        {
            /*周期循环检测注册列表，查找当前任务运行的状态*/
            for(i = 0; i < TASK_POWER_NUM; i++)
            {
                if( power_man_list[i].task_id != 0 )
                {
                    if( power_man_list[i].save_state  == state )
                    {
                        dete_num += 1;
                    }
                }
            }
            if( power_man_count_get() == dete_num )
            {
                ret = 1;
            }
        }
        else
        {
            power_id = power_man_power_id_get(   task_name );
            if( power_id < TASK_POWER_NUM )
            {
                if( power_man_list[power_id].task_id != 0 )
                {
                    if( power_man_list[power_id].save_state  == state )
                    {
                        ret = 1;
                    }
                }
            }
        }
    }
    
    if(osR != 0)
    {
        gaoz_mutex_unlock( power_man_mutex );
    }
    return ret;
}

/** ****************************************************************************
* @remarks       static uint8_t sys_acc_state(void)
* @brief         获取ACC状态
* @param[in]     无
* @param[out]    无
* @return        返回ACC的状态 SYS_KL15_HIGH           SYS_KL15_LOW 
* @attention     无
*******************************************************************************/
static uint8_t power_man_sys_acc_state(void)
{
    return sys_switch_state_get( SYS_KL15 );
}

/** ****************************************************************************
* @remarks       static uint8_t power_man_sys_veh_state(void)
* @brief         获取车辆状态
* @param[in]     无
* @param[out]    无
* @return        返回车辆的状态 SYS_WVEH_START             SYS_WVEH_STOP 
* @attention     无
*******************************************************************************/
static uint8_t power_man_sys_veh_state(void)
{
    return sys_switch_state_get( SYS_WVEH );
}

/** ****************************************************************************
* @remarks       static uint8_t power_man_sys_rtc_state(void)
* @brief         获取RTC状态
* @param[in]     无
* @param[out]    无
* @return        返回RTC的状态 
* @attention     无
*******************************************************************************/
static uint8_t power_man_sys_rtc_state(void)
{
    return sys_switch_state_get( SYS_WRTC   );
}

/** ****************************************************************************
* @remarks       static uint8_t power_man_sys_ble_state(void)
* @brief         获取BLE状态
* @param[in]     无
* @param[out]    无
* @return        返回BLE的状态 
* @attention     无
*******************************************************************************/
static uint8_t power_man_sys_ble_state(void)
{
    return sys_switch_state_get( SYS_WBLE );
}

/** ****************************************************************************
* @remarks       static uint8_t power_man_sys_net_state(void)
* @brief         获取NET状态
* @param[in]     无
* @param[out]    无
* @return        返回NET的状态 
* @attention     无
*******************************************************************************/
static uint8_t power_man_sys_net_state(void)
{
    return sys_switch_state_get( SYS_WNET );
}

/** ****************************************************************************
* @remarks       static uint8_t power_man_sys_gsen_state(void)
* @brief         获取GSEN状态
* @param[in]     无
* @param[out]    无
* @return        返回GSEN的状态  
* @attention     无
*******************************************************************************/
static uint8_t power_man_sys_gsen_state(void)
{
    return sys_switch_state_get( SYS_WGSEN );
}

/** ****************************************************************************
* @remarks       static uint8_t sys_acc_state(void)
* @brief         获取CAN状态
* @param[in]     无
* @param[out]    无
* @return        返回CAN的状态 
* @attention     无
*******************************************************************************/
static uint8_t power_man_sys_can_state(void)
{
    return sys_switch_state_get( SYS_WCAN );
}

/** ****************************************************************************
* @remarks       static uint8_t SYS_WTIME(void)
* @brief         获取TIME状态
* @param[in]     无
* @param[out]    无
* @return        返回CAN的状态 
* @attention     无
*******************************************************************************/
static uint8_t power_man_sys_time_state(void)
{
    return sys_switch_state_get( SYS_WTIME );
}

/** ****************************************************************************
* @remarks       static gsen_bmi160_body_state_e power_man_sys_body_state(void)
* @brief         获取车身状态
* @param[in]     无
* @param[out]    无
* @return        返回CAN的状态 
* @attention     无
*******************************************************************************/
static gsen_bmi160_body_state_e power_man_sys_body_state(void)
{
    return gsen_sensor_body_st_get();
}

/** ****************************************************************************
* @remarks       static uint32_t power_man_sys_wakeup_source_init_state(void)
* @brief         获取系统所有唤醒源
* @param[in]     无
* @param[out]    无
* @return        pdPASS 成功 pdFAIL  失败
* @attention     无
*******************************************************************************/
static uint32_t power_man_sys_wakeup_source_init_state( void )
{
    uint8_t wakeup_num = 0;
    
    if( sys_switch_state_get( SYS_WCAN ) == SYS_WCAN_INIT )
    {
        wakeup_num++;
    }
    if( sys_switch_state_get( SYS_WRTC ) == SYS_WRTC_INIT )
    {
        wakeup_num++;
    }
    if( sys_switch_state_get( SYS_WNET ) == SYS_WNET_INIT )
    {
        wakeup_num++;
    }
    if( sys_switch_state_get( SYS_WBLE ) == SYS_WBLE_INIT )
    {
        wakeup_num++;
    }
    if( sys_switch_state_get( SYS_WGSEN ) == SYS_WGSEN_INIT )
    {
        wakeup_num++;
    }
    if( sys_switch_state_get( SYS_WTIME ) == SYS_WTIME_INIT )
    {
        wakeup_num++;
    }
    if( wakeup_num != SYS_WAKEUP_SOURCE_NUM )
    {
        return pdFAIL;
    }
    
    return pdPASS;
}

/** ****************************************************************************
* @remarks       static void power_man_sys_wakeup_source_sleeping( void )
* @brief         设置唤醒源处于休眠过程中
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void power_man_sys_wakeup_source_sleeping( void )
{
    if( sys_switch_state_get( SYS_WCAN ) != SYS_WCAN_INIT )
    {
        sys_switch_state_set( SYS_WCAN , SYS_WCAN_SLEEPING );
    }
    if( sys_switch_state_get( SYS_WRTC ) != SYS_WRTC_INIT )
    {
        sys_switch_state_set( SYS_WRTC  , SYS_WRTC_SLEEPING );
    }
    if( sys_switch_state_get( SYS_WNET ) != SYS_WNET_INIT )
    {
       sys_switch_state_set( SYS_WNET , SYS_WNET_SLEEPING );
    }
    if( sys_switch_state_get( SYS_WBLE ) != SYS_WBLE_INIT )
    {
        sys_switch_state_set( SYS_WBLE , SYS_WBLE_SLEEPING );
    }
    if( sys_switch_state_get( SYS_WGSEN ) != SYS_WGSEN_INIT )
    {
        sys_switch_state_set( SYS_WGSEN , SYS_WGSEN_SLEEPING );
    }
    if( sys_switch_state_get( SYS_WTIME ) != SYS_WTIME_INIT )
    {
        sys_switch_state_set( SYS_WTIME , SYS_WTIME_SLEEPING );
    }
}

/** ****************************************************************************
* @remarks       static void power_man_sys_wakeup_source_clear(void)
* @brief         清空系统唤醒源信息
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void power_man_sys_wakeup_source_clear(void)
{
    sys_switch_state_set( SYS_WCAN , SYS_WCAN_INIT );
    sys_switch_state_set( SYS_WRTC  , SYS_WRTC_INIT );
    sys_switch_state_set( SYS_WNET , SYS_WNET_INIT );
    sys_switch_state_set( SYS_WBLE , SYS_WBLE_INIT );
    sys_switch_state_set( SYS_WGSEN , SYS_WGSEN_INIT );
    sys_switch_state_set( SYS_WTIME , SYS_WTIME_INIT );
    memset((uint8_t *)&power_man_wakeup_source_list , 0x00 , sizeof(power_man_wakeup_source_list_t));
}

/** ****************************************************************************
* @remarks       static uint32_t power_man_bat_sleep_mode( void )
* @brief         等待BAT进入休眠模式
* @param[in]     无
* @param[out]    无
* @return        pdFAIL 没有进入休眠模式 pdPASS 已经进入休眠模式
* @attention     无
*******************************************************************************/
static uint32_t power_man_bat_sleep_mode( void )
{
	uint32_t ret = pdFAIL;
    power_man_sys_run_state_set( TASK_SLEEP_MODE , (uint8_t *)BAT_MAN_NAME );                /*设置任务中注册电源管理的任务处于运行状态*/
    if( power_man_sys_runing_state_check( TASK_SLEEP_MODE , (uint8_t *)BAT_MAN_NAME ) == 1 ) /*系统处于运行模式*/
    {
        ret = pdPASS;
    }
	return ret;
}

/** ****************************************************************************
* @remarks       static uint32_t power_man_net_run_mode( void )
* @brief         设置NET任务处于运行状态
* @param[in]     无
* @param[out]    无
* @return        pdFAIL 没有进入运行模式 pdPASS 已经进入运行模式
* @attention     无
*******************************************************************************/
static uint32_t power_man_net_run_mode( void )
{
    uint32_t ret = pdFAIL;
    power_man_sys_run_state_set( TASK_RUN_MODE , (uint8_t *)NET_MAN_NAME );                   
    if( power_man_sys_runing_state_check( TASK_RUN_MODE , (uint8_t *)NET_MAN_NAME ) == 1 )
    {
        ret = pdPASS;
    }
    return ret;
}

/** ****************************************************************************
* @remarks       static uint32_t power_man_can_sleep_mode( void )
* @brief         等待CAN进入休眠模式
* @param[in]     无
* @param[out]    无
* @return        pdFAIL 没有进入休眠模式 pdPASS 已经进入休眠模式
* @attention     无
*******************************************************************************/
static uint32_t power_man_can_sleep_mode( void )
{
	uint32_t ret = pdFAIL;
    power_man_sys_run_state_set( TASK_SLEEP_MODE , (uint8_t *)MCU_MAN_NAME );                
    if( power_man_sys_runing_state_check( TASK_SLEEP_MODE , (uint8_t *)MCU_MAN_NAME ) == 1 )
    {
        ret = pdPASS;
    }
	return ret;
}

/** ****************************************************************************
* @remarks       static uint32_t power_man_can_run_mode( void )
* @brief         等待CAN进入运行模式
* @param[in]     无
* @param[out]    无
* @return        pdFAIL 没有进入运行模式 pdPASS 已经进入运行模式
* @attention     无
*******************************************************************************/
static uint32_t power_man_can_run_mode( void )
{
	uint32_t ret = pdFAIL;
    power_man_sys_run_state_set( TASK_RUN_MODE , (uint8_t *)MCU_MAN_NAME );                
    if( power_man_sys_runing_state_check( TASK_RUN_MODE , (uint8_t *)MCU_MAN_NAME ) == 1 )
    {
        ret = pdPASS;
    }
	return ret;
}

/** ****************************************************************************
* @remarks       static uint32_t power_man_gsen_run_mode( void )
* @brief         等待GSEN进入运行模式
* @param[in]     无
* @param[out]    无
* @return        pdFAIL 没有进入运行模式 pdPASS 已经进入运行模式
* @attention     无
*******************************************************************************/
static uint32_t power_man_gsen_run_mode( void )
{
	uint32_t ret = pdFAIL;
    power_man_sys_run_state_set( TASK_RUN_MODE , (uint8_t *)GSEN_MAN_NAME );                
    if( power_man_sys_runing_state_check( TASK_RUN_MODE , (uint8_t *)GSEN_MAN_NAME ) == 1 )
    {
        ret = pdPASS;
    }
	return ret;
}

/** ****************************************************************************
* @remarks       static uint32_t power_man_ble_run_mode( void )
* @brief         等待BLE进入运行模式
* @param[in]     无
* @param[out]    无
* @return        pdFAIL 没有进入运行模式 pdPASS 已经进入运行模式
* @attention     无
*******************************************************************************/
static uint32_t power_man_ble_run_mode( void )
{
	uint32_t ret = pdFAIL;
    power_man_sys_run_state_set( TASK_RUN_MODE , (uint8_t *)BLE_MAN_NAME );                
    if( power_man_sys_runing_state_check( TASK_RUN_MODE , (uint8_t *)BLE_MAN_NAME ) == 1 )
    {
        ret = pdPASS;
    }
	return ret;
}

/** ****************************************************************************
* @remarks       static uint32_t power_man_ble_sleep_mode( void )
* @brief         等待BLE进入睡眠模式
* @param[in]     无
* @param[out]    无
* @return        pdFAIL 没有进入睡眠模式 pdPASS 已经进入睡眠模式
* @attention     无
*******************************************************************************/
static uint32_t power_man_ble_sleep_mode( void )
{
	uint32_t ret = pdFAIL;
    power_man_sys_run_state_set( TASK_SLEEP_MODE , (uint8_t *)BLE_MAN_NAME );                
    if( power_man_sys_runing_state_check( TASK_SLEEP_MODE , (uint8_t *)BLE_MAN_NAME ) == 1 )
    {
        ret = pdPASS;
    }
	return ret;
}

/** ****************************************************************************
* @remarks       static uint32_t power_man_gps_run_mode( void )
* @brief         等待GPS进入运行模式
* @param[in]     无
* @param[out]    无
* @return        pdFAIL 没有进入运行模式 pdPASS 已经进入运行模式
* @attention     无
*******************************************************************************/
static uint32_t power_man_gps_run_mode( void )
{
	uint32_t ret = pdFAIL;
    power_man_sys_run_state_set( TASK_RUN_MODE , (uint8_t *)GPS_MAN_NAME );                
    if( power_man_sys_runing_state_check( TASK_RUN_MODE , (uint8_t *)GPS_MAN_NAME ) == 1 )
    {
        ret = pdPASS;
    }
	return ret;
}

/** ****************************************************************************
* @remarks       void power_man_wakeup_source_set( wakeup_source_e source )
* @brief         设置系统的唤醒管理列表
* @param[in]     source 系统的唤醒源
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void power_man_wakeup_source_set( wakeup_source_e source )
{
    uint32_t osR = 0;
    if( power_wakeup_mutex != 0)
    {
        gaoz_mutex_lock( power_wakeup_mutex );
        osR = 1;
    }
    if( power_man_wakeup_source_list.wakeup_source == POWER_INIT )
    {
        power_man_wakeup_source_list.wakeup_source = source;
        power_man_wakeup_source_list.wakeup_event.event = 0;
        power_man_wakeup_source_list.task_run_num = 0; 
    }
    if( source == POWER_RTC )
    {
        power_man_wakeup_source_list.wakeup_event.rtc = 1;
        sys_switch_state_set( SYS_WRTC , SYS_WRTC_RUN );
    }
    else if( source == POWER_BLE )
    {
        power_man_wakeup_source_list.wakeup_event.ble = 1;
        sys_switch_state_set( SYS_WBLE , SYS_WBLE_RUN );
    }
    else if( source == POWER_CAN )
    {
        power_man_wakeup_source_list.wakeup_event.can = 1;
        sys_switch_state_set( SYS_WCAN , SYS_WCAN_RUN );
    }
    else if( source == POWER_GSEN )
    {
        power_man_wakeup_source_list.wakeup_event.gsen = 1;
        sys_switch_state_set( SYS_WGSEN , SYS_WGSEN_RUN );
    }
    else if( source == POWER_NET )
    {
        power_man_wakeup_source_list.wakeup_event.net = 1;
        sys_switch_state_set( SYS_WNET , SYS_WNET_RUN );
    }
    else if( source == POWER_TIME )
    {
        power_man_wakeup_source_list.wakeup_event.time = 1;
        sys_switch_state_set( SYS_WTIME , SYS_WTIME_RUN );
    }

    if(osR != 0)
    {
        gaoz_mutex_unlock( power_wakeup_mutex );
    }
}

/** ****************************************************************************
* @remarks       uint32_t power_man_wakeup_source_get( wakeup_event_t *event )
* @brief         获取系统唤醒列表事件
* @param[in]     无
* @param[out]    *event 唤醒事件
* @return        无
* @attention     无
*******************************************************************************/
uint32_t power_man_wakeup_source_get( wakeup_source_e source )
{   
    uint32_t ret = pdFAIL;
    uint32_t osR = 0;
    if( power_wakeup_mutex != 0)
    {
        gaoz_mutex_lock( power_wakeup_mutex );
        osR = 1;
    }
    
    if( sys_work_mode_get() == SYS_ST_WAKE )  
    {
        if( source == POWER_RTC )
        {
            if( power_man_wakeup_source_list.wakeup_event.rtc == 1 )
            {
                sys_switch_state_set( SYS_WRTC , SYS_WRTC_SLEEP );
                power_man_wakeup_source_list.wakeup_event.rtc = 0;
                ret = pdPASS;
            }
        }
        else if( source == POWER_BLE )
        {
            if( power_man_wakeup_source_list.wakeup_event.ble == 1 ) 
            {
                sys_switch_state_set( SYS_WBLE , SYS_WBLE_SLEEP );
                power_man_wakeup_source_list.wakeup_event.ble = 0;
                ret = pdPASS;
            }
        }
        else if( source == POWER_CAN )
        {
            if( power_man_wakeup_source_list.wakeup_event.can == 1 )
            {
                sys_switch_state_set( SYS_WCAN , SYS_WCAN_SLEEP );
                power_man_wakeup_source_list.wakeup_event.can = 0;
                ret = pdPASS;
            }
        }
        else if( source == POWER_GSEN )
        {
            if( power_man_wakeup_source_list.wakeup_event.gsen == 1 )
            {
                sys_switch_state_set( SYS_WGSEN , SYS_WGSEN_SLEEP );
                power_man_wakeup_source_list.wakeup_event.gsen = 0;
                ret = pdPASS;
            }
        }
        else if( source == POWER_NET )
        {
            if( power_man_wakeup_source_list.wakeup_event.net == 1 )
            {
                sys_switch_state_set( SYS_WNET , SYS_WNET_SLEEP );
                power_man_wakeup_source_list.wakeup_event.net = 0;
                ret = pdPASS;
            }
        }
        else if( source == POWER_TIME )
        {
            if( power_man_wakeup_source_list.wakeup_event.time == 1 )
            {
                sys_switch_state_set( SYS_WTIME , SYS_WTIME_SLEEP );
                power_man_wakeup_source_list.wakeup_event.time = 0;
                ret = pdPASS;
            }
        }
    }
    
    if(osR != 0)
    {
        gaoz_mutex_unlock( power_wakeup_mutex );
    }
    return ret;
}

/** ****************************************************************************
* @remarks       void power_man_wakeup_task_num_run( void )
* @brief         唤醒任务运行
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/ 
void power_man_wakeup_task_num_run( void )
{
    uint32_t osR = 0;
    uint32_t task_id;
    uint8_t i = 0;
    
    if( power_wakeup_mutex != 0)
    {
        gaoz_mutex_lock( power_wakeup_mutex );
        osR = 1;
    }

    // task_id = gaoz_get_thread_id();

    // for(i = 0; i < 50; i++)
    // {
    //     if(task_dog_t[i].task_id == task_id)
    //     {
    //         task_id = i; 
    //         break;
    //     }
    // }
 
    // log_debug_printf(INFOR_MODE,"800E" ,"power_man_wakeup_task_num_run:%s\r\n",task_dog_t[task_id].task_name);
    
    if( sys_work_mode_get() == SYS_ST_WAKE )  
    {
        if( power_man_wakeup_source_list.task_run_num < 0xFFFFFFFF )
        {
            power_man_wakeup_source_list.task_run_num++;
        }
        power_man_sleep_time_manage.run_over_make = WAKEUP_TIME_INIT;
        power_man_sleep_time_manage.run_tick = gaoz_get_systick();  
    }
    
    if(osR != 0)
    {
        gaoz_mutex_unlock( power_wakeup_mutex );
    }
    
}

/** ****************************************************************************
* @remarks       void power_man_wakeup_task_num_stop( void )
* @brief         唤醒任务停止
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void power_man_wakeup_task_num_stop( void )
{
    uint32_t osR = 0;
    
    if( power_wakeup_mutex != 0)
    {
        gaoz_mutex_lock( power_wakeup_mutex );
        osR = 1;
    }
    
    if( sys_work_mode_get() == SYS_ST_WAKE )  
    {
        if( power_man_wakeup_source_list.task_run_num != 0 )
        {
            power_man_wakeup_source_list.task_run_num--;
        }
        power_man_sleep_time_manage.sleep_over_make = WAKEUP_TIME_INIT;
        power_man_sleep_time_manage.sleep_tick = gaoz_get_systick();
    }
    
    if(osR != 0)
    {
        gaoz_mutex_unlock( power_wakeup_mutex );
    }
    
}

/** ****************************************************************************
* @remarks       void power_man_wakeup_task_num_get( void )
* @brief         唤醒任务获取
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
uint32_t power_man_wakeup_task_num_get( void )
{   
    uint32_t ret = 0;
    uint32_t osR = 0;
    if( power_wakeup_mutex != 0)
    {
        gaoz_mutex_lock( power_wakeup_mutex );
        osR = 1;
    }
    
    ret = power_man_wakeup_source_list.task_run_num;
    
    if(osR != 0)
    {
        gaoz_mutex_unlock( power_wakeup_mutex );
    }
    
    return ret;
}

/** ****************************************************************************
* @remarks       void power_man_wakeup_task_num_clean( void )
* @brief         唤醒任务清楚
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void power_man_wakeup_task_num_clean( void )
{
    uint32_t osR = 0;
    if( power_wakeup_mutex != 0)
    {
        gaoz_mutex_lock( power_wakeup_mutex );
        osR = 1;
    }
    if( sys_work_mode_get() == SYS_ST_WAKE )  
    {   
        power_man_wakeup_source_list.task_run_num = 0;
        power_man_sleep_time_manage_init();
    }
    if(osR != 0)
    {
        gaoz_mutex_unlock( power_wakeup_mutex );
    }
}

/** ****************************************************************************
* @remarks       static void power_man_sleep_time_manage_init( void)
* @brief         系统休眠时间初始化
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void power_man_sleep_time_manage_init( void)
{
    power_man_sleep_time_manage.run_over_make = WAKEUP_TIME_INIT;
    power_man_sleep_time_manage.run_tick = gaoz_get_systick();  
    power_man_sleep_time_manage.sleep_over_make = WAKEUP_TIME_INIT;
    power_man_sleep_time_manage.sleep_tick = gaoz_get_systick();
}

/** ****************************************************************************
* @remarks       static void power_man_run_time_manage_init( void)
* @brief         系统运行时间初始化
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void power_man_run_time_manage_init( void)
{
    power_man_sleep_time_manage.run_over_make = WAKEUP_TIME_INIT;
    power_man_sleep_time_manage.run_tick = gaoz_get_systick();  
}

/** ****************************************************************************
* @remarks       static uint32_t power_man_sys_wakeup_sleep_timeout(void)
* @brief         等待系统唤醒后强制休眠的时间
* @param[in]     无
* @param[out]    无
* @return        pdPASS 成功 pdFAIL  失败
* @attention     无
*******************************************************************************/
static uint32_t power_man_sys_wakeup_sleep_timeout(void)
{
    uint32_t ret = pdFAIL;
    if( power_man_sleep_time_manage.sleep_over_make == WAKEUP_TIME_OUT )
    {
        ret = pdPASS;
    }
    else
    {
        if( pdPASS == sys_timestamp_delay(( uint32_t * )&power_man_sleep_time_manage.sleep_tick, POWER_MAN_WAKEUP_SLEEP_TIME_PERIOD ) )
        {
            power_man_sleep_time_manage.sleep_over_make = WAKEUP_TIME_OUT;
            ret = pdPASS;
        }
    }
    return ret;
}

/** ****************************************************************************
* @remarks       static uint32_t power_man_sys_wakeup_run_timeout(void)
* @brief         等待系统唤醒后运行的时间
* @param[in]     无
* @param[out]    无
* @return        pdPASS 成功 pdFAIL  失败
* @attention     无
*******************************************************************************/
static uint32_t power_man_sys_wakeup_run_timeout(void)
{
    uint32_t ret = pdFAIL; 
    if( power_man_sleep_time_manage.run_over_make == WAKEUP_TIME_OUT )
    { 
        ret = pdPASS;
    }
    else
    {
         if( pdPASS == sys_timestamp_delay(( uint32_t * )&power_man_sleep_time_manage.run_tick, POWER_MAN_WAKEUP_RUN_TIME_PERIOD ) )
        {
             power_man_sleep_time_manage.run_over_make = WAKEUP_TIME_OUT;
            ret = pdPASS;
        }
    }
    return ret;
}

/** ****************************************************************************
* @remarks       static void power_task_manager( void * pvParameters)
* @brief         系统电源管理任务
* @param[in]     * pvParameters 任务参数
* @param[out]    无
* @return        无
* @attention     系统共分为3中工作状态
*                1、运行 : 所有任务全速运行
*                2、休眠 : 模式下根据系统休眠等级的配置不同预留的功能不相同
*                    一级休眠 : 条件预留
*                    二级休眠 : 条件预留
*                    三级休眠 : 条件预留
*                3、停止 : 所有系统模块停止工作，保留系统电源检测功能，定时唤醒系统。
*******************************************************************************/
static void power_task_manager( void * pvParameters)
{
    (void) pvParameters;
    system_run_t system_run_st = SYSTEM_RUN_INIT;
    power_man_sys_t power_sleep_st = POWER_SLEEP_INIT;
    volatile uint32_t gnss_filter_count = 0;
    volatile uint32_t power_man_sem_time = POWER_MAN_SAM_REV_RUN_TIME;
    uint32_t sys_sleep_wait_time = 0;
    uint8_t sys_acc_on_flag = 0;
    mon_reg_task_dog( 60 * GAOZ_TICK_PERIOD_S ,(uint8_t * )POW_MAN_NAME , TASK_SEM_YES );
    log_debug_send(DEBUG_MODE,POW_MAN_NAME ,"Power management task started successfully\r\n",0);
    for( ;; )
    {   
        mon_reset_task_dog();
        /*使用信号量进行延时，其目的是为了快速响应事件,单位延时时间为1S钟*/
        power_man_semaphore_get( power_man_sem_time );
        power_man_sem_time = POWER_MAN_SAM_REV_RUN_TIME;
        /**************************KL15上电 通知所有注册电源管理的任务全速运行***********************************/
        if(( power_man_sys_acc_state() == SYS_KL15_HIGH   ) || ( power_man_sys_veh_state() == SYS_WVEH_START ))                  /*系统处于全速运行状态*/
        {
            power_sleep_st = POWER_SLEEP_INIT;
            sys_sleep_wait_time = gaoz_get_systick();
            sys_acc_on_flag = 1;
            power_man_sys_run_state_set( TASK_RUN_MODE , (uint8_t *)SYS_ALL_NAME );    /*设置任务中注册电源管理的任务处于运行状态*/
            if( power_man_sys_runing_state_check( TASK_RUN_MODE , (uint8_t *)SYS_ALL_NAME ) == 1 ) /*系统处于运行模式*/
            {
                if( sys_work_mode_get() != SYS_ST_RUN )
                {                                                           /*当系统处于运行模式时，有可能会出发系统复位操作。如果不做判断复位限制有可能出现复位被设置时出现置位*/
                    system_run_st = SYSTEM_RUN_START;
                    power_man_sys_wakeup_source_clear();
                    log_debug_send(INFOR_MODE,POW_MAN_NAME,"<<<<<<<<<<*****SYSTEM goes into RUN mode*****>>>>>>>>>>\r\n", 0);
                    sys_work_mode_set(SYS_ST_RUN);                          /*系统处于运行模式*/
                }
            }
        }
        /************************KL15下电 通知所有注册电源管理的任务进行休眠************************************/
        if(( power_man_sys_acc_state() == SYS_KL15_LOW ) && ( power_man_sys_veh_state() != SYS_WVEH_START ))
        {
            /*车辆熄火后继续运行70s确保定位数据能正常上传*/
            if(sys_acc_on_flag == 1)
            {
                if((gaoz_get_systick() - sys_sleep_wait_time) < SLEEP_WITE_TIMEOUT)
                {
                    continue;
                }
                else
                {
                    sys_acc_on_flag = 0;
                }
            }
            
            /* 查询系统唤醒源的状态，查询强制唤醒的时间*/
            if( power_man_sys_wakeup_source_init_state() == pdPASS ) 
            {
                if( system_run_st == SYSTEM_RUN_START )
                {
                    switch( power_sleep_st )
                    {
                        case POWER_SLEEP_INIT:
                            power_sleep_st = POWER_SLEEP_DALAY;
                            power_man_sleep_time_tick = gaoz_get_systick(); 
                            gnss_filter_count = gnss_static_filter_count_get();
                            power_man_semaphore_put();
                        break;
                        case POWER_SLEEP_DALAY:
                           
                            if( ( gnss_filter_count != gnss_static_filter_count_get() ) || 
                                ( pdPASS == sys_timestamp_delay(( uint32_t * )&power_man_sleep_time_tick, POWER_MAN_TIME_PERIOD ) ))
                            {
                                power_sleep_st = POWER_SLEEP_BLE;
                            }
                        break;
                        case POWER_SLEEP_BLE:
                            if( pdPASS == power_man_ble_sleep_mode())
                            {
                                power_sleep_st = POWER_SLEEP_CAN;
                            }
                        break;   
                        case POWER_SLEEP_CAN:
                            if( pdPASS == power_man_can_sleep_mode() )
                            {
                                power_sleep_st = POWER_SLEEP_BAT;
                            }
                        break;
                        case POWER_SLEEP_BAT:
                            if( pdPASS == power_man_bat_sleep_mode() )
                            {
                                power_sleep_st = POWER_SLEEP_SYS;
                            }
                        break;
                        case POWER_SLEEP_SYS:
                            power_man_sys_run_state_set( TASK_SLEEP_MODE , (uint8_t *)SYS_ALL_NAME );                   /*设置任务中注册电源管理的任务处于运行状态*/
                            if( power_man_sys_runing_state_check( TASK_SLEEP_MODE , (uint8_t *)SYS_ALL_NAME ) == 1 ) /*系统处于运行模式*/
                            {
                                if( sys_work_mode_get() != SYS_ST_SLEEP )
                                {    
                                    log_debug_send(INFOR_MODE,POW_MAN_NAME,"<<<<<<<<<<*****SYSTEM goes into SLEEP mode 1*****>>>>>>>>>>\r\n", 0);
                                    sys_work_mode_set(SYS_ST_SLEEP);                          /*系统处于运行模式*/
                                    power_man_sem_time = POWER_MAN_SAM_REV_STOP_TIME;
                                }
                            }
                        break;
                    }
                }
                else
                {
                    power_sleep_st = POWER_SLEEP_INIT;
                    power_man_sys_run_state_set( TASK_RUN_MODE , (uint8_t *)SYS_ALL_NAME );    /*设置任务中注册电源管理的任务处于运行状态*/
                    if( power_man_sys_runing_state_check( TASK_RUN_MODE , (uint8_t *)SYS_ALL_NAME ) == 1 ) /*系统处于运行模式*/
                    {
                        if( sys_work_mode_get() != SYS_ST_RUN )
                        {                                                           /*当系统处于运行模式时，有可能会出发系统复位操作。如果不做判断复位限制有可能出现复位被设置时出现置位*/
                            system_run_st = SYSTEM_RUN_START;
                            power_man_sys_wakeup_source_clear();
                            log_debug_send(INFOR_MODE,POW_MAN_NAME,"<<<<<<<<<<*****SYSTEM goes into RUN mode*****>>>>>>>>>>\r\n", 0);
                            sys_work_mode_set(SYS_ST_RUN);                          /*系统处于运行模式*/
                        }
                    }
                }
            }
            else
            { 
                /*查询RTC 系统唤醒*/
                if( power_man_sys_rtc_state() == SYS_WRTC_WAKE )
                {
                    power_sleep_st = POWER_SLEEP_INIT;
                    if( pdPASS == power_man_net_run_mode() )
                    {
                        log_debug_send(INFOR_MODE,POW_MAN_NAME,"weak up reason : SYS_WRTC_WAKE\r\n", 0);
                        power_man_sleep_time_manage_init();
                        power_man_wakeup_source_set( POWER_RTC );
                    }
                }
                /*查询BLE 系统唤醒*/
                else if( power_man_sys_ble_state() == SYS_WBLE_WAKE )
                {
                    power_sleep_st = POWER_SLEEP_INIT;
                    if( pdPASS == power_man_ble_run_mode() )
                    {
                        log_debug_send(INFOR_MODE,POW_MAN_NAME,"weak up reason : SYS_WBLE_WAKE\r\n", 0);
                        power_man_wakeup_source_set( POWER_BLE );
                        power_man_sleep_time_manage_init();
                    }
                }
                /*查询NET 系统唤醒*/
                else if( power_man_sys_net_state() == SYS_WNET_WAKE )
                {
                    power_sleep_st = POWER_SLEEP_INIT;
                    if(( pdPASS == power_man_can_run_mode()) && ( pdPASS == power_man_net_run_mode() ))
                    {
                        log_debug_send(INFOR_MODE,POW_MAN_NAME,"weak up reason : SYS_WNET_WAKE\r\n", 0);
                        power_man_wakeup_source_set( POWER_NET );
                        power_man_sleep_time_manage_init();
                    }
                }
                /*查询GSEN 系统唤醒*/
                else if( power_man_sys_gsen_state() == SYS_WGSEN_WAKE )
                {
                    power_sleep_st = POWER_SLEEP_INIT;
                    if( pdPASS ==  power_man_gsen_run_mode(  ) )
                    {
                        log_debug_send(INFOR_MODE,POW_MAN_NAME,"weak up reason : SYS_WGSEN_WAKE\r\n", 0);
                        power_man_wakeup_source_set( POWER_GSEN );
                        power_man_sleep_time_manage_init();
                    }
                }
                /*查询CAN 系统唤醒*/
                else if( power_man_sys_can_state() == SYS_WCAN_WAKE )
                {
                    power_sleep_st = POWER_SLEEP_INIT;
                    if(( pdPASS == power_man_can_run_mode()) && ( pdPASS == power_man_net_run_mode() ))
                    {
                        log_debug_send(INFOR_MODE,POW_MAN_NAME,"weak up reason : SYS_WCAN_WAKE\r\n", 0);
                        power_man_wakeup_source_set( POWER_CAN );
                       
                        power_man_sleep_time_manage_init();
                    }
                }
                /*查询TIME 系统唤醒*/
                else if( power_man_sys_time_state() == SYS_WTIME_WAKE )
                {   
                    power_sleep_st = POWER_SLEEP_INIT;
                    if(( pdPASS == power_man_net_run_mode()) && ( pdPASS == power_man_can_run_mode()))
                    {   
                        log_debug_send(INFOR_MODE,POW_MAN_NAME,"weak up reason : SYS_WTIME_WAKE\r\n", 0);
                        power_man_wakeup_source_set( POWER_TIME );
                        power_man_sleep_time_manage_init();
                    }
                }
                /*查询车身状态是否处于振动*/
                else if( power_man_sys_body_state() == GSEN_ATT_ST_CRASH )
                {
                    if(( pdPASS == power_man_can_run_mode()) && ( pdPASS == power_man_net_run_mode() ))
                    {
                        log_debug_send(INFOR_MODE,POW_MAN_NAME,"weak up reason : GSEN_ATT_ST_CRASH\r\n", 0);
                        
                        power_man_sleep_time_manage_init();
                    }
                }
                else if( power_man_sys_body_state() == GSEN_ATT_ST_ROLLOVER )
                {
                    if(( pdPASS == power_man_can_run_mode()) && ( pdPASS == power_man_net_run_mode() ))
                    {
                        log_debug_send(INFOR_MODE,POW_MAN_NAME,"weak up reason : GSEN_ATT_ST_ROLLOVER\r\n", 0);

                        power_man_sleep_time_manage_init();
                    }
                }
                else if( power_man_sys_body_state() == GSEN_ATT_ST_SHAKE )
                {
                    if(( pdPASS == power_man_can_run_mode()) && ( pdPASS == power_man_net_run_mode() ))
                    {
                        log_debug_send(INFOR_MODE,POW_MAN_NAME,"weak up reason : GSEN_ATT_ST_SHAKE\r\n", 0);

                        power_man_sleep_time_manage_init();
                    }
                }
                /*查询车身状态是否处于移位*/
                else if( power_man_sys_body_state() == GSEN_ATT_ST_SHIFTING )
                {
                    if( pdPASS == power_man_gps_run_mode() )
                    {
                        log_debug_send(INFOR_MODE,POW_MAN_NAME,"weak up reason : GSEN_ATT_ST_SHIFTING\r\n", 0);
                                       
                        power_man_sleep_time_manage_init();
                        event_input_data_set( EVENT_VEHICLE , 1 );
                    }
                }
                else
                {    
                    power_sleep_st = POWER_SLEEP_INIT;
                    /*检测系统唤醒后任务运行的数量，当数量为0时则进入电源管理*/
                    if( power_man_wakeup_task_num_get() == 0 )
                    {
                        if( power_man_sys_wakeup_run_timeout() == pdPASS )
                        {
                              if( pdPASS == power_man_bat_sleep_mode() )
                            {
                                 power_man_sys_run_state_set( TASK_SLEEP_MODE , (uint8_t *)SYS_ALL_NAME );                   /*设置任务中注册电源管理的任务处于运行状态*/
                                power_man_sys_wakeup_source_sleeping();
                            #if 0
                                if( power_man_sys_runing_state_check( TASK_SLEEP_MODE , (uint8_t *)CAN_MAN_NAME ) == 1 ) 
                                {
                                    sys_switch_state_set( SYS_WCAN , SYS_WCAN_INIT );
                                }
                                if( power_man_sys_runing_state_check( TASK_SLEEP_MODE , (uint8_t *)NET_MAN_NAME ) == 1 ) 
                                {
                                    sys_switch_state_set( SYS_WNET , SYS_WNET_INIT );
                                }
                                if( power_man_sys_runing_state_check( TASK_SLEEP_MODE , (uint8_t *)GSEN_MAN_NAME ) == 1 ) 
                                {
                                    sys_switch_state_set( SYS_WGSEN , SYS_WGSEN_INIT );
                                }
                                if( power_man_sys_runing_state_check( TASK_SLEEP_MODE , (uint8_t *)BLE_MAN_NAME ) == 1 ) 
                                {
                                    sys_switch_state_set( SYS_WBLE , SYS_WBLE_INIT );
                                }
                            #endif
                                if( power_man_sys_runing_state_check( TASK_SLEEP_MODE , (uint8_t *)SYS_ALL_NAME ) == 1 ) /*系统处于运行模式*/
                                {
                                    if( sys_work_mode_get() != SYS_ST_SLEEP ) 
                                    {
                                        power_man_sys_wakeup_source_clear();
                                        log_debug_send(INFOR_MODE,POW_MAN_NAME,"<<<<<<<<<<*****SYSTEM goes into SLEEP mode 2*****>>>>>>>>>>\r\n", 0);
                                        sys_work_mode_set(SYS_ST_SLEEP);                                 /*系统处于运行模式*/
                                        power_man_sem_time = POWER_MAN_SAM_REV_STOP_TIME;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                           power_man_run_time_manage_init();   /*由于系统被唤醒时，再次进入休眠需要等待一段时间*/
                        if( power_man_sys_wakeup_sleep_timeout() == pdPASS )
                        {
                            if( pdPASS == power_man_bat_sleep_mode() )
                            {
                                power_man_sys_run_state_set( TASK_SLEEP_MODE , (uint8_t *)SYS_ALL_NAME );                   /*设置任务中注册电源管理的任务处于运行状态*/
                                power_man_sys_wakeup_source_sleeping();
                            #if 0    
                                if( power_man_sys_runing_state_check( TASK_SLEEP_MODE , (uint8_t *)CAN_MAN_NAME ) == 1 ) 
                                {
                                    sys_switch_state_set( SYS_WCAN , SYS_WCAN_INIT );
                                }
                                if( power_man_sys_runing_state_check( TASK_SLEEP_MODE , (uint8_t *)NET_MAN_NAME ) == 1 ) 
                                {
                                    sys_switch_state_set( SYS_WNET , SYS_WNET_INIT );
                                }
                                if( power_man_sys_runing_state_check( TASK_SLEEP_MODE , (uint8_t *)GSEN_MAN_NAME ) == 1 ) 
                                {
                                    sys_switch_state_set( SYS_WGSEN , SYS_WGSEN_INIT );
                                }
                                if( power_man_sys_runing_state_check( TASK_SLEEP_MODE , (uint8_t *)BLE_MAN_NAME ) == 1 ) 
                                {
                                    sys_switch_state_set( SYS_WBLE , SYS_WBLE_INIT );
                                }
                            #endif
                                if( power_man_sys_runing_state_check( TASK_SLEEP_MODE , (uint8_t *)SYS_ALL_NAME ) == 1 ) /*系统处于运行模式*/
                                {
                                    if( sys_work_mode_get() != SYS_ST_SLEEP )
                                    {
                                        power_man_sys_wakeup_source_clear();
                                        log_debug_send(INFOR_MODE,POW_MAN_NAME,"<<<<<<<<<<*****SYSTEM goes into SLEEP mode***** 3>>>>>>>>>>\r\n", 0);
                                        sys_work_mode_set(SYS_ST_SLEEP);                                 /*系统处于运行模式*/
                                        power_man_sem_time = POWER_MAN_SAM_REV_STOP_TIME;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

